package com.woniu.order;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class OrderRabbitInit {

    //创建交换机
    @Bean
    public Exchange ttlOrderExchange(){
        return new DirectExchange("ttlOrderExchange");
    }

    //创建队列
    @Bean
    public Queue ttlOrderQueue(){
        Map<String, Object> params = new HashMap<>();
        // 设置死信交换机
        params.put("x-dead-letter-exchange","xdlExchange");
        // 设置路由key
        params.put("x-dead-letter-routing-key","xdl_order");
        // 设置队列的过期时间
        params.put("x-message-ttl", 1000 * 60 * 5);
        return new Queue("ttlOrderQueue",true,false,false,null);
    }

    //ttl绑定
    @Bean
    public Binding ttlBinding(@Qualifier("ttlOrderExchange") Exchange exchange, @Qualifier("ttlOrderQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("ttl_order").noargs();
    }

    //创建交换机
    @Bean
    public Exchange xdlOrderExchange(){
        return new DirectExchange("xdlOrderExchange");
    }

    //创建队列
    @Bean
    public Queue xdlOrderQueue(){
        return new Queue("xdlOrderQueue",true,false,false,null);
    }

    //死信队列绑定死信交换机
    @Bean
    public Binding xdlBinding(@Qualifier("xdlOrderExchange") Exchange exchange, @Qualifier("xdlOrderQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("xdl_order").noargs();
    }
}
